package cn.itsource.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.User;
import cn.itsource.query.UserQuery;
import cn.itsource.service.IUserService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 增加/修改
     * @param user
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="user:add", name = "用户增加",descs = "用户增加")
    public R addORdel(@RequestBody User user){
        try {
            if (user.getId() == null){
                //盐值
                String salt = RandomUtil.randomString(32);
                //密码+盐值
                String md5pswd = SecureUtil.md5(user.getPassword() + salt);
                user.setSalt(salt);
                user.setPassword(md5pswd);
                userService.add(user);
            }else {
                userService.update(user);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="user:del", name = "用户删除",descs = "用户删除")
    public R del(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="user:delList", name = "用户批量删除",descs = "用户批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            userService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(sn="user:getOne", name = "用户获取单个",descs = "用户获取单个")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(userService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(sn="user:getAll", name = "用户查询所有",descs = "用户查询所有")
    public R getAll(){
        try {
            return R.ok(userService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="user:pageQuery", name = "用户分页查询",descs = "用户分页查询")
    public PageInfo<User> pageQuery(@RequestBody UserQuery query){
        return userService.pageQuery(query);
    }

}
